package com.dcjt.firehotcarpad.model

import android.content.Context
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import com.aries.library.fast.manager.LoggerManager
import com.aries.library.fast.retrofit.FastObserver
import com.aries.library.fast.util.FastStackUtil
import com.dcjt.firehotcarpad.entity.BaseEntity
import com.dcjt.firehotcarpad.entity.CheckLogin
import com.dcjt.firehotcarpad.entity.SetCLZTBean
import com.dcjt.firehotcarpad.manager.DataManager
import com.dcjt.firehotcarpad.retrofit1.repository.ApiRepository1
import com.dcjt.firehotcarpad.utils.LoginStatusUtils
import com.google.gson.Gson
import io.reactivex.disposables.CompositeDisposable
import java.util.zip.DataFormatException

class FunctionViewModel : ViewModel() {
    private val mCompositeDisposable = CompositeDisposable()
    val requestStatusLiveData: MutableLiveData<BaseEntity<Any>> = MutableLiveData()
    fun changeCarStatus(zqid: String, cjid: String, zt: String, ywjq: Boolean, context: Context) {
        var data: HashMap<String, Any>? = null
        //有警情
        data = hashMapOf<String, Any>(
            Pair(
                "cldata", Gson().toJson(
                    hashMapOf(
                        Pair("clid", DataManager.getInstance().user.account),//wjq
                        Pair("zqid", zqid),//y
                        Pair("cjid", cjid),//
                        Pair("cllxdm", DataManager.getInstance().user.cllxdm),//
                        Pair("clzzzt", zt),//wjq
                        Pair("dept_id", DataManager.getInstance().user.dept_id)
                    )
                )
            )
        )

        ApiRepository1.getInstance().changeCarStatus(data, ywjq)
            .subscribe(object : FastObserver<SetCLZTBean>() {
                override fun _onError(errorRes: Int, errorCode: Int, e: Throwable) {
                    super._onError(errorRes, errorCode, e)
                    if (e is DataFormatException) {
                        requestStatusLiveData.postValue(BaseEntity<Any>().also {
                            it.data = null
                            it.code = BaseEntity.ok
                            LoggerManager.e("changeCarStatus 修改成功")
                            SendMessageUtils.get().sendMessage(
                                DataManager.getInstance().appConfigEntity.jcj_pmqEnum.jcJ_Cmd.buZ_CLZT_SET,
                                zqid,
                                context,
                                ""
                            )
                        })
                    } else {
                        LoggerManager.e("changeCarStatus 修改失败 $errorCode")
                        requestStatusLiveData.postValue(BaseEntity<Any>().also {
                            it.data = null
                            it.code = BaseEntity.err
                        })
                    }
                }

                override fun _onNext(entity: SetCLZTBean) {
                    requestStatusLiveData.postValue(BaseEntity<Any>().also {
                        it.data = null
                        it.code = BaseEntity.ok
                        LoggerManager.e("changeCarStatus 修改成功")
                        SendMessageUtils.get().sendMessage(
                            DataManager.getInstance().appConfigEntity.jcj_pmqEnum.jcJ_Cmd.buZ_CLZT_SET,
                            zqid,
                            context,
                            entity.upclxx
                        )
                    })

                }
            })

    }
    fun checkUserLogin() {
        LoggerManager.e("checkUserLogin----------")
        ApiRepository1.getInstance().checkUserLogin()
            .subscribe(object : FastObserver<CheckLogin>() {
                override fun _onError(errorRes: Int, errorCode: Int, e: Throwable) {
                    super._onError(errorRes, errorCode, e)

                }

                override fun _onNext(entity: CheckLogin) {
                    LoggerManager.e("checkUserLogin----------1")
//                    if (!entity.chkval!!){
//                        //未登录，跳转登录页面  默认登录
//                        val activity = FastStackUtil.getInstance().current
//                        if (activity.javaClass.simpleName == "com.dcjt.firehotcarpad.moudle.LoginActivity") {
//                            return
//                        }
//                        LoginStatusUtils.loginOut(activity, true)
//                    }
                }
            })

    }

    /**
     * 清空任务容器
     */
    override fun onCleared() {
        super.onCleared()
        mCompositeDisposable.dispose()
    }

}